//
//  VisitorLoginView.swift
//  weibo项目
//
//  Created by mac on 16/1/11.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
//给按钮设置代理  
 protocol VisitorLoginViewDelegate:NSObjectProtocol{

//    在代理中写两个方法
//用户将要注册
    func userWillRegister()
//    用户将要˙登录
    func userWillLogin()


}


class VisitorLoginView: UIView {

//    代理用weak修饰,防止循环引用
   weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
    
    func setUpUIWithInfo(imageName:String?,tipString:String){
        if let img = imageName {
//            当图片的名字是必选的时候,才可以走这个方法
            circleIcon.image = UIImage(named: img)
            largeHouse.hidden = true
//            把遮罩的图片隐藏
//            backView.hidden = true
//            把圆圈放到图层的最上面
            bringSubviewToFront(circleIcon)
            tipLabel.text = tipString

        }
        else{
            
        startAnimation()
            
        
        }
        
    
    }
    
//    让图片可以旋转
    func startAnimation(){
        
//        设置基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
//        设置动画的次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
//        持续时间
        base.duration = 10
        base.removedOnCompletion = false
        circleIcon.layer.addAnimation(base, forKey: nil)
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI() {
        
        
        addSubview(circleIcon)
    
          addSubview(largeHouse)
    addSubview(backView)
        addSubview(LoginBtn)
        addSubview(tipLabel)
        addSubview(register)
        
      
       
//        给子控件添加约束
     circleIcon.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(225)
//            make.height.equalTo(40)
        }
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        LoginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        register.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        backView.snp_makeConstraints { (make) -> Void in
//            make.left.equalTo(circleIcon.snp_left)
//            make.top.equalTo(circleIcon.snp_top)
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(tipLabel.snp_bottom)
        }
    
    self.backgroundColor = UIColor(white: 0.93, alpha: 1)
    
//    gei登录设置事件
    LoginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
    
    register.addTarget(self, action: "registerClick", forControlEvents: .TouchUpInside)
    
    }
   
//    
    @objc private func loginBtnClick(){
    
    visitorViewDelegate?.userWillLogin()
    
    }
    
 @objc  private  func registerClick(){
    
     visitorViewDelegate?.userWillRegister()
        
    }
    
    //懒加载所有的子视图
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var tipLabel: UILabel = {
      let l = UILabel()
       l.font = UIFont.systemFontOfSize(18)
        l.text = "登录后,别人评论你的微薄别人评论你的微薄别人评论你的微"
        l.numberOfLines = 0;
        l.textAlignment = .Left
        l.textColor = UIColor.darkGrayColor()
        l.sizeToFit()
        return l
    }()
    
    lazy var LoginBtn : UIButton = {
        
    let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: .Normal)
       loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(18)
        
        loginBtn.sizeToFit()
        
        return loginBtn
    
    
    }()
    
    lazy var register:UIButton = {
    let register = UIButton()
        
        register.titleLabel?.font = UIFont.systemFontOfSize(18)
        register.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        register.setTitle("注册", forState: .Normal)
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        register.sizeToFit()
        
        return register
    }()
    

}


















